package com.song.view.gis;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @(#)OLClusterAlgrithm.java
 * 
 * @description: OL Style聚类cluster算法 聚集算法 需要的数据格式 $RECSEG $ATTRSEG分隔的 id name
 *               lon lat， 左右下上的extent resolution zoom 返回的结果 json数据 旧的js接收到的数据
 *               LON LAT NAME EID CHANNEL ID STATE
 * @author: 杨朝晖 2013/01/04
 * @version: 1.0
 * @modify: MODIFIER'S NAME YYYY/MM/DD 修改内容简述
 * @Copyright: 版权信息
 */
public class OLClusterAlgrithm {
	// 要素数组
	protected List features = new ArrayList<Feature>();
	// 聚类数组
	protected List clusters = new ArrayList<Cluster>();;
	protected Double resolution;
	private String ismaxzoom = "0";
	/**
	 * 读入数据
	 *@return void
	 *@History: 无
	 */
	public void readData(List list, Double minx, Double maxx, Double miny,
			Double maxy, Double resolution) {
		this.resolution = resolution;
		this.features = list;
	}

	/**
	 * 聚类算法逻辑
	 * 
	 *@param
	 *@return void
	 *@History: 无
	 */
	public List cluster() {
		List listTmp = new ArrayList<ClusterFeatureEle>();
		for (int i = 0, len = features.size(); i < len; i++) {
			Cluster cluster;
			Feature feature;
			feature = (Feature) features.get(i);
			Boolean clustered = false;
			for (int j = 0, jlen = clusters.size(); j < jlen; j++) {
				cluster = (Cluster) clusters.get(j);
				if (shouldCluster(cluster, feature)) {
					addToCluster(cluster, feature);
					clustered = true;
					break;
				}
			}
			if (!clustered) {
				clusters.add(createCluster((Feature) features.get(i)));
			}
		}
		if (this.clusters.size() > 0) {
			ClusterFeatureEle cfe = null;
			Cluster clusterTmp = null;
			Double lonTmp, latTmp;
			for (int k = 0, klen = this.clusters.size(); k < klen; k++) {
				cfe = new ClusterFeatureEle();
				clusterTmp = (Cluster) clusters.get(k);
				if (clusterTmp.getSize() > 1) {
					lonTmp = clusterTmp.getCenterLon();
					latTmp = clusterTmp.getCenterLat();
					cfe.setLon(lonTmp);
					cfe.setLat(latTmp);
					cfe.setType("cluster");
					cfe.setSize(clusterTmp.getSize());
					cfe.setIncludeFeatures(clusterTmp);
				} else {
					Iterator clusterTmpIter = clusterTmp.getFeatures()
							.iterator();
					Feature featureT = null;
					while (clusterTmpIter.hasNext()) {
						featureT = (Feature) clusterTmpIter.next();
						cfe.setOneFeature(featureT);
						cfe.setType("point");
						cfe.setSize(1);
					}
				}
				listTmp.add(cfe);
			}
		}
		return listTmp;
	}

	/**
	 * 创建聚类
	 *@param
	 *@return void
	 *@History: 无
	 */
	private Cluster createCluster(Feature feature) {
		Cluster ct = new Cluster();
		ct.push(feature);
		return ct;
	}

	/**
	 * 添加到聚类
	 * 
	 *@param
	 *@return void
	 *@History: 无
	 * 
	 */
	private void addToCluster(Cluster cluster, Feature feature) {
		cluster.push(feature);
	}

	/**
	 * 是否该聚类
	 *@param
	 *@return void
	 *@History: 无
	 */
	private boolean shouldCluster(Cluster cluster, Feature feature) {
		Boolean flg = false;
		Double clon = cluster.getCenterLon();
		Double clat = cluster.getCenterLat();
		Double distance = Math.sqrt(Math
				.pow((clon - feature.getJd()), 2)
				+ Math.pow((clat - feature.getWd()), 2));
		flg = (distance <= this.resolution);
		return flg;
	}

	public String getIsmaxzoom() {
		return ismaxzoom;
	}

	public void setIsmaxzoom(String ismaxzoom) {
		this.ismaxzoom = ismaxzoom;
	}
}
